package com.soufang.soufangdemo.service.impl;

import com.soufang.soufangdemo.base.BusinessException;
import com.soufang.soufangdemo.base.Status;
import com.soufang.soufangdemo.dto.SubwayDTO;
import com.soufang.soufangdemo.dto.SubwayStationDTO;
import com.soufang.soufangdemo.dto.SupportAddressDTO;
import com.soufang.soufangdemo.entity.SubwayStation;
import com.soufang.soufangdemo.entity.SupportAddress;
import com.soufang.soufangdemo.repository.SubwayRepository;
import com.soufang.soufangdemo.repository.SubwayStationRepository;
import com.soufang.soufangdemo.repository.SupportAddressRepository;
import com.soufang.soufangdemo.service.AddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl implements AddressService {

    private final SupportAddressRepository addressRepository;
    private final SubwayRepository subwayRepository;
    private final SubwayStationRepository subwayStationRepository;

    public AddressServiceImpl(SupportAddressRepository addressRepository,
                              SubwayRepository subwayRepository,
                              SubwayStationRepository subwayStationRepository) {
        this.addressRepository = addressRepository;
        this.subwayRepository = subwayRepository;
        this.subwayStationRepository = subwayStationRepository;
    }

    @Override
    public List<SupportAddressDTO> findAllCities() {
        return addressRepository.findAllByLevel(SupportAddress.Level.CITY.getValue())
                .stream()
                .map(a -> {
                    SupportAddressDTO dto = new SupportAddressDTO();
                    BeanUtils.copyProperties(a, dto);
                    return dto;
                }).collect(Collectors.toList());
    }

    @Override
    public List<SupportAddressDTO> findAllRegionsByCityName(String cityName) {
        return addressRepository.findAllByLevelAndBelongTo(SupportAddress.Level.REGION.getValue(), cityName)
                .stream()
                .map(a -> {
                    SupportAddressDTO dto = new SupportAddressDTO();
                    BeanUtils.copyProperties(a, dto);
                    return dto;
                }).collect(Collectors.toList());
    }

    @Override
    public List<SubwayDTO> findAllSubwaysByCityName(String cityEnName) {
        SupportAddress city = addressRepository.findByEnNameAndLevel(cityEnName, SupportAddress.Level.CITY.getValue());
        if (city == null) {
            throw new BusinessException(Status.CITY_NOT_FOUND);
        }
        return subwayRepository.findAllByCityEnName(cityEnName).stream().map(s -> {
            SubwayDTO dto = new SubwayDTO();
            BeanUtils.copyProperties(s, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SubwayStationDTO> getStationsByCityEnNameAndSubwayId(String cityEnName, Long subwayId) {
        SupportAddress city = addressRepository.findByEnNameAndLevel(cityEnName, SupportAddress.Level.CITY.getValue());
        if (city == null) {
            throw new BusinessException(Status.CITY_NOT_FOUND);
        }
        List<SubwayStation> stations = subwayStationRepository.findAllBySubwayId(subwayId);
        return stations.stream().map(s -> {
            SubwayStationDTO dto = new SubwayStationDTO();
            BeanUtils.copyProperties(s, dto);
            return dto;
        }).collect(Collectors.toList());
    }

}
